package com.bootx.service.predict;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 方法一：平均值法（基础统计）
 * 思路：对每个 order，计算其历史的平均 amount 和 openTime。
 */
public class MeanPredictor {

    public List<Prediction> predict(List<RedPackageRound> rounds) {
        Map<Integer, List<Double>> amountMap = new HashMap<>();
        Map<Integer, List<Integer>> timeMap = new HashMap<>();

        for (RedPackageRound round : rounds) {
            for (RedPackageItem item : round.items) {
                amountMap.computeIfAbsent(item.order, k -> new ArrayList<>()).add(item.amount);
                timeMap.computeIfAbsent(item.order, k -> new ArrayList<>()).add(item.openTime);
            }
        }

        List<Prediction> predictions = new ArrayList<>();
        for (int order = 1; order <= 10; order++) {
            List<Double> amounts = amountMap.getOrDefault(order, List.of());
            List<Integer> times = timeMap.getOrDefault(order, List.of());
            double meanAmount = amounts.stream().mapToDouble(a -> a).average().orElse(0.0);
            int meanTime = (int) times.stream().mapToInt(t -> t).average().orElse(0);
            predictions.add(new Prediction(order, meanAmount, meanTime));
        }

        return predictions;
    }
}
